# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('libffi' 'libffi-devel')
pkgver=v3.2.1.r142.g17ffc36
pkgrel=1
pkgdesc="Portable, high level programming interface to various calling conventions"
arch=('i686' 'x86_64')
url="http://sourceware.org/libffi/"
groups=('libraries')
license=('MIT')
depends=()
checkdepends=('dejagnu')
#options=('!strip' 'debug')
install=libffi.install
source=("libffi"::"git+git://github.com/atgreen/libffi.git"
        config.sub
        config.guess
        0001-msys2ize.patch)
sha1sums=('SKIP'
          '17c4f8628ea435c999be66ee4c2967fdaf3b2955'
          'ffb6644dbad00413c668c833e3f3680d1d73aa53'
          '474dc283b662917ffca4dd3c631f77b4a3c7ee3f')

pkgver() {
  cd ${pkgname}
  git describe --long | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd ${pkgname}
  cp ${srcdir}/config.sub .
  cp ${srcdir}/config.guess .
  git am ${srcdir}/0001-msys2ize.patch

  autoreconf -fi
}

build() {
  cd ${pkgname}
  # Perform the build outside of srcdir so that
  # srcdir is not simplified to '.' as relative
  # paths in debug information are not ideal.
  [ -d build ] && rm -rf build
  mkdir build
  cd build
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-debug")
  fi
  ${srcdir}/${pkgname}/configure \
    --prefix=/usr \
    --srcdir=${srcdir}/${pkgname} \
    --build=${CHOST} \
    --enable-static \
    --enable-shared \
    "${extra_config[@]}"
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd ${pkgname}/build
  make check
}

package_libffi() {
  mkdir -p $pkgdir/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${pkgname}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libffi-devel() {
  pkgdesc="Libffi headers and libraries"
  groups=('development')
  if check_option "debug" "y"; then
    options=('staticlibs' '!strip' 'debug')
  else
    options=('staticlibs' '!strip' 'debug')
  fi
  depends=("libffi=${pkgver}")

  mkdir -p ${pkgdir}/usr
  #cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
